//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// Object properties dialog - 'Exit' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "RgnProperties.h"
# include "Map.h"
# include "GarminTypes.h"
# include "ChangeHighway.h"
# include "SetRgnExtra.h"
# include "Button.h"
# include "ListView.h"
# include "Settings.h"

void CRgnExitsPage::_FillHighway (const highway_t * _pHighway) {
	if (_pHighway && ! _pHighway->IsDeleted ())
		SetDlgItemText (IDC_HIGHWAY, _pHighway->strName.c_str ());
	else
		SetDlgItemText (IDC_HIGHWAY, "-");
}

bool CRgnExitsPage::OnInit (HWND _hwndFocus) {
	const bool bIsExit = g_map.pTypeSet->pfIsHwExit (m_rgn);

	// Street/description.
	if (bIsExit && m_rgn.pExtra && ! m_rgn.pExtra->strStreetDesc.empty ())
		SetDlgItemText (IDC_STREET, m_rgn.pExtra->strStreetDesc.c_str ());
	if (! bIsExit)
		EnableDlgItem (IDC_STREET, false);

	// Highway.
	m_pHighway = m_rgn.pExtra ? m_rgn.pExtra->pHighway : NULL;
	_FillHighway (m_pHighway);
	if (! bIsExit || g_map.Highways.empty ()) {
		EnableDlgItem (IDC_HIGHWAY,        false);
		EnableDlgItem (IDC_SELECT_HIGHWAY, false);
		EnableDlgItem (IDC_REMOVE_HIGHWAY, false);
	}
	SetButtonIcon (m_hWnd, IDC_REMOVE_HIGHWAY, IDI_CROSS);

	// Facilities.
	const HWND hList = GetDlgItem (IDC_FACILITIES);

	ListView_SetExtendedListViewStyleEx (hList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

	AddColumn (hList, 0, "Name");
	AddColumn (hList, 1, "Type");
	AddColumn (hList, 2, "Direction");
	AddColumn (hList, 3, "Facilities");
	AutoSizeColumns (hList);
	if (! bIsExit)
		::EnableWindow (hList, FALSE);

	return true;
}

void CRgnExitsPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_SELECT_HIGHWAY:
			ShowChangeHighway (m_hWnd, m_pHighway);
			_FillHighway (m_pHighway);
			PropSheetChanged ();
			break;
		case IDC_REMOVE_HIGHWAY:
			m_pHighway = NULL;
			_FillHighway (m_pHighway);
			PropSheetChanged ();
			break;

		case IDC_STREET:
			if (_wNotifyCode == EN_UPDATE)
				PropSheetChanged ();
			break;
	}
}

void CRgnExitsPage::OnApply (const PSHNOTIFY *) {
	if (g_map.pTypeSet->pfIsHwExit (m_rgn)) {
		// Street/description.
		const HWND hStreet = GetDlgItem (IDC_STREET);
		const size_t cStreetLen = ::GetWindowTextLength (hStreet);
		char * const strStreet = reinterpret_cast<char *> (alloca (cStreetLen + 1));
		::GetWindowText (hStreet, strStreet, cStreetLen + 1);
		if (! (m_rgn.pExtra->strStreetDesc == strStreet))
			SetObjectHWEDescription (m_rgn, strStreet);

		// Highway.
		const highway_t * const pOldHighway = m_rgn.pExtra ? m_rgn.pExtra->pHighway : NULL;
		if (pOldHighway != m_pHighway)
			ChangeHighway (m_rgn, m_pHighway);

		// TODO: Overnight parking.

		// TODO: facilities.
	}
}
